package com.king.headline.UI;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.king.headline.R;
import com.king.headline.base.BaseActivity;
import com.king.headline.beans.UserInfo;
import com.king.headline.cache.CacheUtils;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.util.AccountManager;
import com.king.headline.util.CustomToast;
import com.king.headline.util.KeyBoardUtils;
import com.king.headline.util.UIUtils;

import org.json.JSONObject;

import butterknife.BindView;

/**
 * 登录界面
 *
 * @author Administrator
 */
public class Loging2Activity extends BaseActivity implements OnClickListener {
    @BindView(R.id.bt_loging)
    Button bt_loging;
    @BindView(R.id.et_userName)
    EditText et_userName;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.tv_getPwd)
    TextView tv_getPwd;
    @BindView(R.id.item_main)
    View item_main;
    @BindView(R.id.iv_chebox)
    CheckBox iv_chebox;
    private String password;
    private int checkBoxChoose = 1;//1表示没被选中，2代表被选中
    private String userName;
    private ProgressDialog progressDialog;
    private ProgressDialog pDialog;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UIUtils.addActivity(this);
        baseSetContentView(R.layout.activity_loging);
        init();
        setTopBarAndAction();
    }


    private void init() {
        setBackIcon(true);
        tv_getPwd.setOnClickListener(this);
        bt_loging.setOnClickListener(this);
//        String name = CacheUtils.getString(CacheUtils.NAME, "");
//        String pass = CacheUtils.getString(CacheUtils.PWD, "");
//        et_userName.setText(name);
//        et_password.setText(pass);
        //点击空白处关闭键盘
        item_main.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                KeyBoardUtils.closeKeybord(et_userName);
                return false;
            }
        });

        iv_chebox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    checkBoxChoose = 2;
                } else {
                    checkBoxChoose = 1;
                }
            }
        });
    }

    private void setTopBarAndAction() {
        setBarTitleText("登录"); // 设置Title标题
    }


    private void login() {
        // 获取登录信息
        userName = et_userName.getText().toString();
        password = et_password.getText().toString();

        //是否取消登录
        boolean cancel = false;
        View focusView = null;
        if (cancel) {
            //使无效的输入框获取焦点
            focusView.requestFocus();
        } else {
            //登录
            if (pDialog == null) {
                pDialog = ProgressDialog.show(this, "", UIUtils.getString(R.string.progress_content));
            }
            NetWork.getInstance().login(userName, password, new Callback() {
                @Override
                public void success(String result) {
                    Gson gson = new Gson();
                    UserInfo userInfo = gson.fromJson(result, UserInfo.class);
                    AccountManager.getInstance().setUser(userInfo);
                    CacheUtils.putString(CacheUtils.APIKEY, userInfo.apikey);
                    startActivity(new Intent(Loging2Activity.this, MainUI.class));
                    Loging2Activity.this.finish();
                }

                @Override
                public void notice(JSONObject jsonObject, String message) {
                    if (pDialog != null) {
                        pDialog.dismiss();
                        pDialog = null;
                    }
                    CustomToast.showToast(message);
                }

                @Override
                public void failed(String e) {
                    if (pDialog != null) {
                        pDialog.dismiss();
                        pDialog = null;
                    }
                    CustomToast.showToast(e);
                }
            });
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_loging:
                login();
                break;

            case R.id.tv_getPwd:
                startActivity(new Intent(this,GetPswActivity.class));
                break;

            default:
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.removeActivity(this);
    }


}
